package com.aron;

import com.utils.Audio;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * @version v1.0
 * @ProjectName: studycoding
 * @ClassName: Tank
 * @Description: TODO(玩家坦克实体)
 * @Author: Leo
 * @Date: 2020/6/1 11:39
 */
public class PlayerTank {
    //执行速度
    private static final int SPEED = 20;
    private Logger logger = LoggerFactory.getLogger(PlayerTank.class);
    //坦克的X，Y轴
    private int x, y;
    //记录敌人的上一步的位置
    private int oldX, oldY;
    //坦克的方向
    private Dir dir;
    //坦克的类型（敌人，友军）
    private TankGroup group;
    //坦克是否移动属性
    private boolean moving = false;
    //是否存活
    private boolean isLive = true;
    //移动的方向
    private boolean isL, isU, isR, isD;
    //坦克的宽高
    private int width, height;

    public PlayerTank(int x, int y, Dir dir, TankGroup group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.oldX = x;
        this.oldY = y;
        this.width = ResourseMgr.enemyTankU.getWidth();
        this.height = ResourseMgr.enemyTankU.getHeight();
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    //绘制方块
    public void paint(Graphics g) {
        //logger.info("测试info");
        //logger.error("测试error");
        //logger.debug("测试debug");
        if (!this.isLive) return;
        switch (dir) {
            case UP:
                g.drawImage(ResourseMgr.playerTankU, x, y, null);
                break;
            case LEFT:
                g.drawImage(ResourseMgr.playerTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(ResourseMgr.playerTankR, x, y, null);
                break;
            case DOWN:
                g.drawImage(ResourseMgr.playerTankD, x, y, null);
                break;
        }

        move();
    }

    /**
     * 设置坦克在键盘监听到方向键按下的处理
     *
     * @param e
     */
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                isL = true;
                break;
            case KeyEvent.VK_UP:
                isU = true;
                break;
            case KeyEvent.VK_RIGHT:
                isR = true;
                break;
            case KeyEvent.VK_DOWN:
                isD = true;
                break;

        }
        setMainDir();
    }

    /**
     * 设置坦克在键盘监听到方向键放开的处理
     *
     * @param e
     */
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                isL = false;
                break;
            case KeyEvent.VK_UP:
                isU = false;
                break;
            case KeyEvent.VK_RIGHT:
                isR = false;
                break;
            case KeyEvent.VK_DOWN:
                isD = false;
                break;
            case KeyEvent.VK_SPACE:
                tankFire();
                break;
        }
        setMainDir();
    }

    //导弹发射
    private void tankFire() {
        if (!this.isLive) return;
        int mx = x + ResourseMgr.playerTankU.getWidth() / 2 - ResourseMgr.tankMissile.getWidth() / 2;
        int my = y + ResourseMgr.playerTankU.getHeight() / 2 - ResourseMgr.tankMissile.getHeight() / 2;
        TankFrame.getInstance().addMissile(new Missile(mx, my, this.dir, this.group));
        new Thread(()->new Audio("audio/fire.wav").play()).start();
    }

    private void setMainDir() {
        if (!isL && !isU && !isR && !isD) {
            moving = false;
        } else {
            moving = true;
        }
        if (isL && !isU && !isR && !isD)
            dir = Dir.LEFT;
        if (!isL && isU && !isR && !isD)
            dir = Dir.UP;
        if (!isL && !isU && isR && !isD)
            dir = Dir.RIGHT;
        if (!isL && !isU && !isR && isD)
            dir = Dir.DOWN;
    }

    //设置方向的移动
    private void move() {
        if (!moving) return;
        oldX = x;
        oldY = y;
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        borderLineChk();
    }

    //设置坦克是否到达边界
    private void borderLineChk() {
        if (x < 0 || y < 30 || x + width > TankFrame.GAME_W || y + height > TankFrame.GAME_H) {
            this.tankBack();
        }
    }

    private void tankBack() {
        this.x = oldX;
        this.y = oldY;
    }

    /**
     * 当坦克死亡则设置存活属性为死亡
     */
    public void die() {
        this.setLive(false);
    }


}
